package com.qijian.business.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.business.domain.ProductionDetailSalary;
import com.qijian.business.domain.query.ProductionDetailQuery;
import com.qijian.business.domain.query.ProductionDetailSalaryQuery;
import com.qijian.business.domain.vo.ProductionDetailVo;
import org.apache.ibatis.annotations.Param;
import com.qijian.business.domain.query.ProductionDetailSalaryQuery;
import com.qijian.business.domain.vo.ProductionDetailSalaryVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 报工工资Mapper接口
 *
 * @author qijian
 * @date 2025-03-29
 */
public interface ProductionDetailSalaryMapper extends BaseMapper<ProductionDetailSalary> {

    @Select("select count(0) from production_detail_salary WHERE create_time >= CURRENT_DATE ")
    int countCurrentDateWithDel();

    @Select("SELECT\n" +
            "\tSUM( user_salary ) userSalary,\n" +
            "\tuser_id userId, DATE( create_time ) createTime ,EXTRACT(  DAY  FROM create_time) AS day ,\n" +
            "\tuser_name userName \n" +
            "FROM\n" +
            "\tproduction_detail_salary \n" +
            "WHERE\n" +
            "\t  is_del = 0 and create_time >= DATE_FORMAT(CURDATE() ,'%Y-%m-01')  AND create_time < DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01')\n" +
            "\t   AND user_id = #{userId} and team_leader_check = 1 \n" +
            "GROUP BY\n" +
            "\tuser_id,\n" +
            "\tuser_name,createTime,day ORDER BY team_leader_check DESC")
    List<ProductionDetailSalaryVo> countUserSalary(@Param("userId") Long userId);

    @Select("SELECT\n" +
            "\t IF(team_leader_check,SUM( ifnull(check_qty,0) * ifnull(cur_work_cost,0) ), sum(ifnull(finish_qty,0) * ifnull(cur_work_cost,0) )) userSalary,\n" +
            "\tuser_id userId,\n" +
            "\tteam_leader_check teamLeaderCheck \n" +
            "FROM\n" +
            "\tproduction_detail_salary \n" +
            "WHERE\n" +
            "\t  is_del = 0 and create_time >= DATE_FORMAT(CURDATE() ,'%Y-%m-01')  AND create_time < DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01')\n" +
            "\t   AND user_id = #{userId}\n" +
            "GROUP BY\n" +
            "\t user_id,\n" +
            "\t team_leader_check ORDER BY team_leader_check DESC")
    List<ProductionDetailSalaryVo> listMoonSalary(@Param("userId") Long userId);

    List<ProductionDetailSalary> countUserDaySalary(@Param(Constants.WRAPPER) QueryWrapper<ProductionDetailSalaryQuery> query);


    Page<ProductionDetailSalaryVo> getTeamProduction(@Param("userId") Long userId, @Param(Constants.WRAPPER) QueryWrapper<ProductionDetailSalaryQuery> query, Page<ProductionDetailSalary> page);

    List<ProductionDetailSalaryVo> getTeamSalary(@Param("userId") Long userId, @Param(Constants.WRAPPER) QueryWrapper<ProductionDetailSalaryQuery> query);
}
